/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import database.DBConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.PropertiesManager;

/**
 *
 * @author rosana
 */
public class PessoaDAO  implements OperacoesDAO{
    
     private Connection conexao;

    public PessoaDAO() throws ClassNotFoundException, SQLException {        
            conexao = DBConnection.getConnection();   

    }
    @Override
    public void inserir(Object obj) throws SQLException {
         String SQL = buscaSQL("insert.Pessoa");
        if (obj instanceof Pessoa) {
            Pessoa user = (Pessoa) obj;
            PreparedStatement st = conexao.prepareStatement(SQL);
            st.setString(1, user.getNome());
            st.setString(2, user.getCpf());
            st.setString(3, user.getRg());
            st.setString(4, user.getDataNasc());
            st.setString(5, user.getEndereco());
            st.setString(6, user.getTelefone());
            st.setString(7, user.getCelular());
            st.setString(8, user.getEmail());
            st.execute();
        }
    }

   @Override
    public boolean excluir(Object obj) throws SQLException {
        Integer num = (Integer) obj;
        int id = num.intValue();
        String sql = (String) buscaSQL("delete.Pessoa");
        PreparedStatement stmt = conexao.prepareStatement(sql);
        stmt.setInt(1, id);
        stmt.execute();
        stmt.close();
        return true;
    }

    public void editar(Object obj) throws SQLException {
        Pessoa pes = (Pessoa) obj;
        String sql = (String) buscaSQL("Update.Pessoa");
        PreparedStatement stmt = conexao.prepareStatement(sql);
        stmt.setString(1, pes.getNome());
        stmt.setString(2, pes.getCpf());
        stmt.setString(3, pes.getRg());
        stmt.setString(4, pes.getDataNasc());
        stmt.setString(5, pes.getEndereco());
        stmt.setString(6, pes.getTelefone());
        stmt.setString(7, pes.getCelular());
        stmt.setString(8, pes.getEmail());
        stmt.setInt(9, pes.getIdPessoa());
        stmt.execute();
        stmt.close();
        //return true;
    }

    @Override
    public ArrayList pesquisar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public Pessoa PessoaChave(int chave) throws SQLException {
        Pessoa pes = null;
        String SQL = buscaSQL("SelectById.Pessoa");
        PreparedStatement st = conexao.prepareStatement(SQL);
        st.setInt(1, chave);
        ResultSet res = st.executeQuery();
        while (res.next()) {
            int id = res.getInt(1);
            String nome = res.getString(2);
            String cpf = res.getString(3);
            String rg = res.getString(4);
            String dataNasc = res.getString(5);
            String endereco = res.getString(6);
            String telefone = res.getString(7);
            String celular = res.getString(8);
            String email = res.getString(9);
            pes = new Pessoa(nome, cpf, rg, dataNasc, endereco, telefone, celular, email);
        }
        return pes;
    }
    
     private String buscaSQL(String chave) {
        String sql = null;
        try {
            PropertiesManager pm = new PropertiesManager("sql.properties");
            HashMap dados = pm.readPropertiesFile();
            sql = (String) dados.get(chave);
        } catch (IOException ex) {
            Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sql;
    }
}
